﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Xml;
using Cabal.Kernel.Provider;
using Cabal.Kernel;
using System.Threading;

namespace Cabal
{
    public partial class frmtest : Form
    {
        public frmtest()
        {
            InitializeComponent();


            BS.BS_AlwaysUseable = true;

            //_env = new EnvSupport();
            _kernel = new CabalSoul();
            _env = _kernel.Support;
            _setting = _kernel.Settings;

            BS.BS_AlwaysUseable = false;
        }

        EnvSupport _env;
        Setting _setting;
        CabalSoul _kernel;

        private void button1_Click(object sender, EventArgs e)
        {
            string aaa = "abc";

            aaa = "ssbcc";

            string[] strs = aaa.Split('b');
        }

        private void button2_Click(object sender, EventArgs e)
        {
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(textBox1.Text);

            XmlNode cabalsoul = doc.SelectSingleNode("CabalSoul");
                XmlNode test1 = cabalsoul.SelectSingleNode("Test1");
                    XmlNode test2 = test1.SelectSingleNode("test2");
                XmlNode fuckyou = cabalsoul.SelectSingleNode("fuckyou");
        }

        private void button5_Click(object sender, EventArgs e)
        {
            _env.RegisterXML("test", textBox1.Text);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            MessageBox.Show(_env.ReadString("test", textBox2.Text), textBox2.Text);
        }

        private void button4_Click(object sender, EventArgs e)
        {
            _env.WriteString("test", textBox3.Text, textBox4.Text, "fuck you man", true);
            _env.Save("test", "c:\\shit.xml");
        }

        private void button6_Click(object sender, EventArgs e)
        {
            _env.WriteString("test", textBox3.Text, textBox4.Text, "fuck you man", false);
            _env.Save("test", "c:\\shit.xml");
        }

        private void button7_Click(object sender, EventArgs e)
        {
            _setting = new Setting(_kernel);
        }

        private void button8_Click(object sender, EventArgs e)
        {
            string[] items;
            foreach (string i in textBox5.Lines)
            {
                items = i.Split('=');
                if (items.Length == 2)
                    _setting.WriteString(items[0], items[1]);
            }
            _setting.Save();
        }

        private void button9_Click(object sender, EventArgs e)
        {
            //BS.BS_AlwaysUseable = true;
            BS bs = _kernel.ResControl.BSCollection.RequestBS(chkverf.Checked);
            txtBS.Text = bs.BSValue;
            txtStr2.Text = bs.Str2Exp;
            txtVerf.Text = bs.VerifyCode;
            //MessageBox.Show(string.Format("[BS]\r\n BSV={0}\r\n Str2Exp={1}\r\n GotTime={2}\r\n LastTime={3} \r\n VerifyCode={4}",bs.BSValue,bs.Str2Exp,bs.GotTime,bs.LastTime,bs.VerifyCode));
            //BS.BS_AlwaysUseable = false;
            bs.Used();
        }

        private void button10_Click(object sender, EventArgs e)
        {
            //_kernel.ResControl.BSCollection.Clear("bs");
            //_kernel.ResControl.BSAdd(new BS("bsbsbsshit", "riririririexp", new DateTime(1989, 6, 4, 12, 0, 0)));
            BS bs = new BS(txtBS.Text, txtStr2.Text, DateTime.Now);
            bs.VerifyCode = txtVerf.Text;
            bs.Leaving = 2;
            _kernel.ResControl.BSCollection.AddBS(bs);
            _kernel.ResControl.BSCollection.Save();

            //_kernel.ResControl.UserCollection.PeekUser(
        }

        Thread testt;
        private void button12_Click(object sender, EventArgs e)
        {
            testt = new Thread(new ThreadStart(ttest));
            testt.Start();
        }

        private void button13_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < 10; i++)
                button12_Click(null, null);
        }
        
        void ttest()
        {
            this.Invoke(new ThreadStart(delegate() { timer1.Start(); }));
            for (int i = 0; i < 200; i++)
            {
                try
                {
                    this.Invoke(new ThreadStart(delegate()
                    {
                        //label1.ForeColor = Color.Black;
                        //label1.Text = i.ToString();
                        textBox6.AppendText("[" + i.ToString() + "]\r\n");
                    }));
                }
                catch (Exception err) { }

                Cabal.Kernel.Output.RequestItem item = new Cabal.Kernel.Output.RequestItem("test" + i.ToString());
                item.TargetAddress = new Uri("http://www.codeproject.net/");
                item.Method = "GET";
                item.RequestIP = "self";
                try
                {
                    //Thread.Sleep(1);
                    _kernel.OutputControl.AddRequestEx(item);
                }
                catch(Exception err)
                {
                    if (item.IsWakeUp == true)
                        item.IsWakeUp = false;

                    this.Invoke(new ThreadStart(delegate()
                    {
                        //label1.ForeColor = Color.Red;
                        //label1.Text = err.Message;
                        textBox6.AppendText(err.Message + "\r\n");
                    }));
                }
            }
            this.Invoke(new ThreadStart(delegate()
            {
                timer1.Stop();
                label1.ForeColor = Color.Red;
                label1.Text = "Stopped";
            }));
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            //刷新列表
            listBox1.BeginUpdate();
            listBox1.Items.Clear();

            lock (_kernel.OutputControl.Manager.RunningMission)
            {
                Cabal.Kernel.Output.RequestItem[] v = new Cabal.Kernel.Output.RequestItem[_kernel.OutputControl.Manager.RunningMission.Count];
                _kernel.OutputControl.Manager.RunningMission.Values.CopyTo(v, 0);
                foreach (Cabal.Kernel.Output.RequestItem i in v)
                {
                    if (i != null)
                        listBox1.Items.Add(i);
                }
                //listBox1.Items.AddRange(v);
            }
            listBox1.EndUpdate();
            //看看线程情况
            switch (testt.ThreadState)
            {
                case ThreadState.Running:
                    label1.ForeColor = Color.Green;
                    label1.Text = "Running";
                    break;
                case ThreadState.Stopped:
                    label1.ForeColor = Color.Red;
                    label1.Text = "Stopped";
                    break;
                case ThreadState.WaitSleepJoin:
                    label1.ForeColor = Color.Blue;
                    label1.Text = "Waiting";
                    break;
            }
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            label2.Text = "Result= " + _kernel.OutputControl.Responses.Count;
        }

        private void label2_Click(object sender, EventArgs e)
        {
            //_kernel.OutputControl.Responses;
            timer2.Enabled = !timer2.Enabled;
        }

        private void button13_Click_1(object sender, EventArgs e)
        {
            Clipboard.SetText(dbglogt.GetString());
        }
    }
}
